package com.cr.front.news.dao.repository.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cr.front.news.dao.mapper.NewsMapper;
import com.cr.front.news.dao.repository.NewsRepository;
import com.cr.front.news.pojo.entity.News;
import com.cr.front.news.pojo.query.SearchParam;
import com.cr.front.news.pojo.vo.NewsByCategoryIdVO;
import com.cr.front.news.pojo.vo.NewsByTimeVO;
import com.cr.front.news.pojo.vo.NewsByViewsVO;
import com.cr.front.news.pojo.vo.NewsVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Repository
public class NewsRepositoryImpl implements NewsRepository {

    private static final Logger log = LoggerFactory.getLogger(NewsRepositoryImpl.class);
    @Autowired
    private NewsMapper newsMapper;


    @Override
    public List<NewsByTimeVO> getNewsByTimeRank() {
        log.info("按资讯发布时间查询文章：入参 = 无");
        return newsMapper.selectByTimeRank();
    }

    @Override
    public NewsVO getDetailNewsById(Integer id) {
        log.info("获取文章详情：入参id = {}", id);
        NewsVO newsVO = new NewsVO();
        newsVO = newsMapper.selectById(id);
        log.info("获取文章详情：入参id = {} 出参newsVO:{}",id, newsVO);
        return newsVO;
    }


    @Override
    public List<NewsByViewsVO> getNewsByViews() {
        log.info("按资讯浏览量查询文章：入参 = 无");
        return newsMapper.selectByViews();
    }

    @Override
    public List<News> getNewsByCategoryId(SearchParam searchParam) {
        log.info("按资讯类别查询文章：入参 searchParam = {}", searchParam);
        QueryWrapper<News> queryWrapper = new QueryWrapper<>();
        if (searchParam.getId() > 0 ) {
            queryWrapper.eq("category_id", searchParam.getId());
            queryWrapper.eq("status", 2);//魔法数后期修改为枚举 2 为状态已发布 todo
        }else {
            queryWrapper.eq("status", 2);//魔法数后期修改为枚举 2 为状态已发布 todo
        }
        queryWrapper.orderByDesc("created_time");
//        return newsMapper.selectList(queryWrapper);
        return newsMapper.selectList(queryWrapper);
    }

    @Override
    public List<News> getCacheNews() {
        return newsMapper.selectList(null);
    }

}
